package com.zerdoor.controller;

import com.zerdoor.model.UserInfo;
import com.zerdoor.service.IFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @描述 收藏夹通用controller
 * Created by zhongjieyu on 2014/12/26.
 */
@Controller
@RequestMapping("/favorite")
public class FavoriteController {

    @Autowired
    private IFavoriteService favoriteService;

    /**
     * 添加到收藏夹
     * @param collectId 当前收藏品ID
     * @param type 收藏类别
     * @param request
     * @return 返回ID
     */
    @RequestMapping(value = "/addFavorite", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> addFavorite( int collectId,int type,HttpServletRequest request) {

        try {
            //获取当前用户ID
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            //等于空表示未登录
            if(userInfo == null){

                ResponseEntity<String> responseEntity = new ResponseEntity<String>("未登录！", HttpStatus.INTERNAL_SERVER_ERROR);
                return responseEntity;
            }

            //添加收藏
            String favoriteId =  favoriteService.addFavorite(userInfo.getUserId(),collectId,type);

            ResponseEntity<String> responseEntity = new ResponseEntity<String>(favoriteId, HttpStatus.OK);
            return responseEntity;

        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("error", HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }
    }

    /**
     * 根据收藏品ID和收藏类型查询当前是否被收藏，如果是则返回收藏信息
     * @param collectId 当前收藏品ID
     * @param type 收藏类别
     * @param request
     * @return
     */
    @RequestMapping(value = "/findFavorite", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> findFavorite( int collectId,int type,HttpServletRequest request) {



        try {

            //获取当前用户ID
            UserInfo userInfo = (UserInfo)request.getSession().getAttribute("curUser");

            //查询是否被收藏
            String  favoriteId = favoriteService.findFavorite(userInfo.getUserId(),collectId,type);


            ResponseEntity<String> responseEntity = new ResponseEntity<String>(favoriteId, HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("error", HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

    /**
     * 根据收藏ID删除收藏信息
     * @param favoriteId 收藏ID
     * @return
     */
    @RequestMapping(value = "/removeFavorite", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<?> removeFavorite( int favoriteId,int collectId,int type) {



        try {



            //删除收藏
            favoriteService.deleteFavorite(favoriteId,collectId,type);


            ResponseEntity<String> responseEntity = new ResponseEntity<String>("", HttpStatus.OK);
            return responseEntity;
        } catch (Exception e) {

            e.printStackTrace();

            ResponseEntity<String> responseEntity = new ResponseEntity<String>("error", HttpStatus.INTERNAL_SERVER_ERROR);
            return responseEntity;
        }

    }

}
